<template>
<div>

  <div style="padding: 10px 0">
    <el-input placeholder="请输入用户名" style="width: 200px;" suffix-icon="el-icon-search" v-model="username"></el-input>
    <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
    <el-button  type="warning" @click="reset">重置</el-button>
    <el-button type="primary" @click="handleAdd">新增</el-button>
    <el-popconfirm
        title="您确定批量删除吗？"
        class="ml-5"
        @confirm="delBatch"
    >
      <el-button type="danger"  slot="reference">批量删除<i class="el-icon-remove-outline"></i> </el-button>
    </el-popconfirm>
  </div>
  <el-table :data="tableData" border stripe     @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55">
    </el-table-column>
    <el-table-column prop="userId" label="ID" >
    </el-table-column>
    <el-table-column prop="username" label="用户名" width="140">
    </el-table-column>
    <el-table-column prop="realname" label="真实姓名">
    </el-table-column>
    <el-table-column prop="phone" label="电话">
    </el-table-column>
    <el-table-column label="操作" >
      <template slot-scope="scope">
        <el-button type="primary" @click="handleEdit(scope.row)">编辑<i class="el-icon-edit"></i> </el-button>
        <el-popconfirm
            title="您确定删除吗？"
            class="ml-5"
            @confirm="del(scope.row.userId)"
        >
          <el-button type="danger"  slot="reference">删除<i class="el-icon-remove-outline"></i> </el-button>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <div style="padding: 10px 0">

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </div>
  <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%">
    <el-form label-width="80px" size="small">
      <el-form-item label="用户名" >
        <el-input v-model="form.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" >
        <el-input type="password" show-password v-model="form.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名" >
        <el-input v-model="form.realname" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="手机号" >
        <el-input v-model="form.phone" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: "User",
  data(){

    return {
      tableData:[],
      total:0,
      pageNum:1,
      pageSize:10,
      username:"",
      password:"",
      realname:"",
      phone:"",
      dialogFormVisible:false,
      form:{},
      multipleSelection:[]

    }
  },
  created() {
    this.load()
  },
  methods:{
    load(){
      this.request.get("/user/page",{
        params:{
          pageNum:this.pageNum,
          pageSize:this.pageSize,
          username:this.username
        }
      }).then(res=>{
        console.log(res)
        this.tableData=res.data.records
        this.total=res.data.total
      })
    },
    handleSelectionChange(val){
      this.multipleSelection=val
    },
    handleAdd(){
      this.dialogFormVisible=true
      this.form={}
      console.log(this.form)
    },
    save(){
      this.request.post("/user",this.form).then(res=>{
        if(res){
          this.$message.success("保存成功")
          this.dialogFormVisible=false
          this.load()
        }else{
          this.$message.error("保存失败")
        }
      })
    },
    reset(){
      this.username=""
      this.load()
    },
    del(userId){
      this.request.delete("/user/"+userId).then(res=>{
        if (res) {
          this.$message.success("删除成功")
          this.load()
        }else {
          this.$message.error("删除失败")
        }
      })
    },
    delBatch(){
      let ids=this.multipleSelection.map(v=>v.userId)
      this.request.post("/user/batch/del",ids).then(res=>{
        if (res) {
          this.$message.success("批量删除成功")
          this.load()
        }else {
          this.$message.error("批量删除失败")
        }
      })
    },
    handleEdit(row){
      this.form=row
      this.dialogFormVisible=true
    },
    handleSizeChange(pageSize){
      this.pageSize=pageSize
      this.load()
    },
    handleCurrentChange(pageNum){
      this.pageNum=pageNum
      this.load()
    }
  }
}
</script>

<style scoped>

</style>